﻿function AddDelLike()
{
    if(__P_M=="")
    {
        $Login$();return;
    }
    showProgressBar(true); 
    $("imgLike").click = null;   
    if(gIsLike)
    {
        WebServices.WWW.Files.AddLike(gPageName,onsuccLike);
    }
    else
    {
        WebServices.WWW.Files.DeleteLike(gPageName,onsuccLike);
    }
}
//下载
function Download()
{
   
    window.open("/files/download.htm?DocID="+gPageName);
}
//(Msg="OK"执行成功、="NO"执行失败，Effect文档喜欢的数量，State已喜欢过、！State还未喜欢)
function onsuccLike(result)
{
    if(result.Msg=="OK")
    {
       $("LikeID").innerHTML = result.Effect; 
       if(result.State)
       {
            gIsLike = false;
            $("imgLike").click = AddDelLike;
            $("imgLike").src = "/images/icon_good2.gif";
       }
       else
       {
            gIsLike = true;
            $("imgLike").click = AddDelLike;
            $("imgLike").src = "/images/icon_good.gif";
       }
    }
    showProgressBar(false); 
}
///
function RateOver(value)
{
    RateValue = value;
    UpdateRate();
}
function RateOut()
{
    RateValue = sRateValue;
    UpdateRate();
}
function RateClick(value)
{
    if(__P_M=="")
    {
        $Login$();return;
    }
    tmp = sRateValue;
    sRateValue = value;
    RateValue = value;
    WebServices.WWW.Files.Rate(gPageName,value,RateSucc,RateError);
     
}

function UpdateRate()
{
    for (var i = 1; i < 6; i++) 
    {
        var starElement=$get("ThaiRating_Star_"+i);
        if(starElement==null) return;
        if (RateValue > (i-1)) 
        {
            try
            {
                starElement.setAttribute("class","ratingStar filledRatingStar");
            }
            catch(e)
            {
			    starElement.setAttribute("className","ratingStar filledRatingStar"); 
			} 
        }
        else 
        {
            try
            {
                starElement.setAttribute("class","ratingStar emptyRatingStar");
            }
            catch(e)
            {
			    starElement.setAttribute("className","ratingStar emptyRatingStar");
			}
        }
    }
}
function RateSucc(result)
{
    if(result[0].State)
    {
        var obj1 = $get("ThaiRating");
        var obj2 = $get("RFen");
        var obj3 = $get("RRen");
        obj2.innerHTML = result[1];
        obj3.innerHTML = result[2];
        RateValue = result[3];
        var listBuilder = new Sys.StringBuilder();
        for (var index = 0; index < RateValue; ++index)
        {
            listBuilder.append('<span class="ratingStar savedRatingStar" style="float: left;">&nbsp;</span>');
        }
        for(var index=RateValue;index<5; ++index)
        {
            listBuilder.append('<span class="ratingStar emptyRatingStar" style="float: left;">&nbsp;</span>');
        }
        obj1.innerHTML = listBuilder.toString();
    }
    else
    {
        sRateValue = tmp;
        RateValue = sRateValue;
        UpdateRate();
    }
}
function RateError(result)
{
    sRateValue = tmp;
    RateValue = sRateValue;
    UpdateRate();
}



function Save()
{
  //if(m=="") window.location = "/user/login.htm?IsShow=1&ReturnUrl="+window.location.href;
  if($get("txtComment").value=="") return;
  if(__P_M==""){$Login$();return;}
  $get("btnSave").disabled = true;
  showProgressBar(true); 
  WebServices.WWW.Files.AddComment(gPageName,$get("txtComment").value,10,1,onsucc,onerror);
}
function onsucc(result)
{
    $get("btnSave").disabled = false;
    showProgressBar(false);
    if(result[2].State)
    {
        RendList(result[0],"divComment");
        RendPage(result[1],"divCommentPage",strHome,strPre,strLast,strNext,strSel,strUnSel);
        $get("commentCount").innerHTML="("+result[3]+")";
        $get("txtComment").value = "";
        $get("MessagesBox").style.display='block';
        $get("MessageImg").src = MessageImageS;
        $get("MessageText").innerHTML = MessageTextS;
    }
    else
    {
         if(result[2].Msg =="l")window.location = "/user/login.htm?IsShow=1&ReturnUrl="+window.location.href;
    }
}
function onerror(result)
{

    $get("btnSave").disabled = false;
    $get("MessagesBox").style.display='block';
    $get("MessageImg").src = MessageImageF;
    $get("MessageText").innerHTML = MessageTextF;
}

function RendList(list,divID)
{
    
    var domDiv = $get(divID);
    if(domDiv!=null)
    {
        if($get("commedItem")==null) return;
        var item= $get("commedItem").innerHTML;
        gLastCount = list.length;
        var listBuilder = new Sys.StringBuilder();
        for (var index = 0; index < list.length; ++index) 
        {
            var obj = list[index];
            var tmp = item.replace(/\$Avatar\$/gi,"<img src=\""+(obj.Avatar==""?"/Images/No_pic.gif":obj.Avatar)+"\"/>");
            tmp = tmp.replace(/\$Nickname\$/gi,obj.Nickname).replace(/\$MemberNo\$/gi,obj.MemberNo).replace(/\$Desc\$/gi,EncodeMessage(obj.Desc));
            var arr1 = gDateFormat.split(":");
            var arr2 = obj.DiffLastUpdate.split(":");
            if(arr2[0]>0)
            {
                tmp = tmp.replace(/\$CreateDate\$/gi,gStrDate.replace("{0}",arr2[0]).replace("{1}",arr1[0]));
            }
            else if(arr2[1]>0)
            {
                tmp = tmp.replace(/\$CreateDate\$/gi,gStrDate.replace("{0}",arr2[1]).replace("{1}",arr1[1]));
            }
            else if(arr2[2]>0)
            {
                tmp = tmp.replace(/\$CreateDate\$/gi,gStrDate.replace("{0}",arr2[2]).replace("{1}",arr1[2]));
            }
            else if(arr2[3]>0)
            {
                tmp = tmp.replace(/\$CreateDate\$/gi,gStrDate.replace("{0}",arr2[3]).replace("{1}",arr1[3]));
            }
            else if(arr2[4]>0)
            {
                tmp = tmp.replace(/\$CreateDate\$/gi,gStrDate.replace("{0}",arr2[4]).replace("{1}",arr1[4]));
            }
            else if(arr2[5]>0)
            {
                tmp = tmp.replace(/\$CreateDate\$/gi,gStrDate.replace("{0}",arr2[5]).replace("{1}",arr1[5]));
            }
            else
            {
                tmp = tmp.replace(/\$CreateDate\$/gi,"");
            }
            listBuilder.append(tmp);
        }
        domDiv.innerHTML = listBuilder.toString();
    }
}